<script setup lang="ts">
import { getGoodDetailRankAPI } from '@/apis/detail';
import { GoodInterface } from '@/types/good';
import { computed, ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router';

// 声明props类型
const props = defineProps<{ rankType: '1' | '2' }>();

const route = useRoute();

// 获取热榜信息方法
const rank = ref<Array<GoodInterface>>([]);

// 适配热榜标题名称
const rankTitleList = { 1: '今日榜单', 2: '本周热门' };
const title = computed(() => rankTitleList[`${props.rankType}`]);

// 路由信息变更时重新请求热榜数据
watchEffect(async onCleanup => {
  const { response, controller } = getGoodDetailRankAPI({ id: +route.params.id, type: +props.rankType || 1 });
  // 设置清理回调
  onCleanup(() => controller.abort());
  rank.value = (await response).data.result;
});
</script>

<template>
  <div class="goods-hot">
    <h3>{{ title }}</h3>
    <!-- 商品区块 -->
    <RouterLink :to="`/detail/${item.id}`" class="goods-item" v-for="item in rank" :key="item.id">
      <img :src="item.picture" alt="" />
      <p class="name ellipsis">{{ item.name }}</p>
      <p class="desc ellipsis">{{ item.desc }}</p>
      <p class="price">{{ item.price }}</p>
    </RouterLink>
  </div>
</template>


<style scoped lang="scss">
.goods-hot {
  h3 {
    height: 70px;
    background: $helpColor;
    color: #fff;
    font-size: 18px;
    text-align: center;
    line-height: 70px;
    margin-bottom: 10px;
    font-weight: normal;
  }

  .goods-item {
    width: 100%;
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }
}
</style>